﻿//==============================================================================================
//Author: Uncle Song
//Create Date: 2022-06-17
//Description: 关于帧，插值，和四元数插值的运用
//----------------------------------------------------------------------------------------------
//Alter History:
//             2022-06-17  add Codes.
//============================================================================================== 
using System.Collections;
using UnityEngine;

namespace FrameWorkSong
{
    public class DoRotation : MonoBehaviour
    {
        public Transform Target;

        void Start()
        {
            DoRotationForTime(transform, new Vector3(-60, -160, -60), 3);
        }
        public void DoRotationForTime(Transform transform, Vector3 vector, float time)
        {
            Quaternion targetQua = Quaternion.Euler(vector);
            StartCoroutine(DORotation(time, transform.rotation, targetQua));
        }

        IEnumerator DORotation(float time, Quaternion formQua, Quaternion targetQua)
        {
            float t = 0;
            while (t < 1)
            {
                t += Time.deltaTime / time;
                t = t > 1 ? 1 : t;
                transform.rotation = Quaternion.Lerp(formQua, targetQua, t);
                yield return null;
            }
        }
        //这里有一些思考，下面是之前的错误代码，错误有点，
        //1.每次插值计算都是用新的角度计算，这样只会无限趋近于终点。正确的做法是一直用起点和终点带入。
        //2.每一帧的时间是不同是，不能一次性计算总帧数。
        //3.理解api错误插值的定义，插值传入错误。
        //void Start()
        //{
        //    DoRotationForTime(transform, new Vector3(60, 60, 0), 10);
        //}
        //public void DoRotationForTime(Transform transform, Vector3 vector, float time)
        //{
        //    Quaternion targetQua = Quaternion.Euler(vector);
        //    float count = time / Time.deltaTime;
        //    StartCoroutine(DORotation(count + 1, transform, targetQua));
        //}
        //IEnumerator DORotation(float count, Transform transform, Quaternion targetQua)
        //{
        //    int i = 0;
        //    while (count > i)
        //    {
        //        transform.rotation = Quaternion.Lerp(transform.rotation, targetQua, 10 / count);
        //        Debug.Log(i);
        //        i++;
        //        yield return 1;
        //    }
        //}
    }
}
